package com.dingding.web.wechat.handler;

import java.util.Date;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.dingding.common.core.beans.WechatUser;
import com.dingding.common.core.message.factory.ResponseMessageFactory;
import com.dingding.common.core.message.receivemsg.eventmsg.SubscribeEventMsg;
import com.dingding.common.core.message.responsemsg.AbstractResponseMessage;
import com.dingding.common.core.message.responsemsg.usualmsg.text.TextMsgRsp;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.common.core.util.XMLParse;
import com.dingding.facade.user.service.IWechatUserManageFacade;
import com.dingding.facde.activity.service.IWechatActivityService;


@Component("eventXmlHandler")
public class EventXmlHandler extends AbstractRequestHandler {
	
	private Logger log = Logger.getLogger(this.getClass());

	// 声明活动模块业务逻辑的接口
	@Resource(name = "wechatActivityService")
	private IWechatActivityService wechatActivityService;

	// 微信用户管理
	@Resource(name = "wechatUserManageFacade")
	private IWechatUserManageFacade wechatUserManageFacade;

	/**
	 * @Description: 处理微信请求的xml
	 * @param eventType xml消息类型
	 * @param requestXml 请求的xml具体消息
	 * @author fengshuonan
	 * @return String 返回给服务器的具体xml信息
	 * @since 2.3.0
	 */
	@Override
	public String dispose(String eventType, String requestXml) {
		
		switch (eventType) {

			case "subscribe":// 关注微信公众号
				SubscribeEventMsg subscribeEventMsg = XMLParse.toObject(requestXml, SubscribeEventMsg.class);
				AbstractResponseMessage response = this.subscribe(subscribeEventMsg);
				String xml = XMLParse.toXML(response,"UTF-8");
				log.info("返回给客户端信息：" + xml);
				return xml;
				
			case "unsubscribe":// 取消关注公众号
				break;
				
			case "SCAN": // 扫描已关注的二维码
				break;
				
			case "CLICK":// 点击菜单的事件
				break;
	
			case "VIEW":// 点击跳转链接时的事件
				break;
				
			default:
				break;
		}
		return null;
	}
	
	
	/**
	 * @Description: 处理关注事件的消息
	 * @param subscribeEventMsg 关注事件的请求信息
	 * @return AbstractResponseMessage 关注消息的响应信息（响应给微信服务器）
	 * @author fengshuonan
	 * @since 2.3.0
	 */
	public AbstractResponseMessage subscribe(SubscribeEventMsg subscribeEventMsg){
		
		String eventKey = subscribeEventMsg.getEventKey();
		
		if(ToolUtil.isNotBlank(eventKey) && eventKey.startsWith("qrscene"))
		{
			//如果没有关注过公众号
			if(this.wechatUserManageFacade.validateUserIsSub(subscribeEventMsg.getFromUserName()) == false){
				
				//增加推广商统计次数
				String qrParam = eventKey.substring(8);
				this.wechatActivityService.addShopStatNum(Integer.valueOf(qrParam));
				
				log.info("增加一次推广商的用户关注数量，推广商id：" + qrParam);
				
				//增加这个用户的扫描记录
				this.wechatUserManageFacade.addUserSubscribe(new WechatUser(ToolUtil.getUid(),subscribeEventMsg.getFromUserName(),new Date(),qrParam));
			
				//提示用户点击注册
				TextMsgRsp responseMessage = new ResponseMessageFactory().createMessage(subscribeEventMsg.getToUserName(), subscribeEventMsg.getFromUserName(), "text");
				
				responseMessage.setContent("感谢您的关注，请点击右下角“下载app”菜单，进行注册!");
				
				return responseMessage;
			}
		}
			
		return null;
	}

}
